package com.seari.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.seari.bean.AlarmInfor;
import com.seari.bean.Configure;
import com.seari.bean.OCCStatus;
import com.seari.bean.SelectObj;
import com.seari.bean.UserRoles;
import com.seari.pojo.DataGridBean;
import com.seari.pojo.MessageBean;
import com.seari.service.DevStatusService;
import com.seari.service.UserService;
import com.seari.utils.ParseUtils;
import com.seari.utils.StringHelper;

/**
 * 设备状态监控
 * 
 * @author hfm
 *
 */
@Controller
@RequestMapping("devStatus")
public class DevStatusController {

	@Autowired
	private DevStatusService devStatusService;

	@Autowired
	private UserService userService;

	/**
	 * 网络管理>>>网络状态监控
	 * 
	 * @return
	 */
	@RequestMapping("/alarm/getOCCStatus")
	@ResponseBody
	public MessageBean getOCCStatus() {
		MessageBean mb = new MessageBean();
		Map<String, Object> params = new HashMap<>();
		List<OCCStatus> list = devStatusService.getOCCStatus(params);
		if (list != null && list.size() > 0) {
			mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
			mb.setMessage("网络状态监控");
			mb.setData(list);
		} else {
			mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
		}
		return mb;
	}

	/**
	 * 报警管理>>>报警综合信息 站台下拉框
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getStation")
	@ResponseBody
	public Map<String, Object> getStation() {
		List<SelectObj> list = devStatusService.getStation();
		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}

	/**
	 * 报警管理>>>报警综合信息 状态下拉框
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getStatus")
	@ResponseBody
	public Map<String, Object> getStatus() {
		List<SelectObj> list = devStatusService.getStatus();
		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}

	/**
	 * 报警管理>>>报警综合信息 详情 维修方式下拉框
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getMaintenanceStatus")
	@ResponseBody
	public Map<String, Object> getMaintenanceStatus() {
		List<SelectObj> list = devStatusService.getMaintenanceStatus();
		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}

	/**
	 * 报警管理>>>报警综合信息 详情 工单状态下拉框
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getTaskStatus")
	@ResponseBody
	public Map<String, Object> getTaskStatus() {
		List<SelectObj> list = devStatusService.getTaskStatus();
		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}

	/**
	 * 报警管理>>>报警综合信息 有分页
	 * 
	 * @return
	 */
	@RequestMapping("/alarm/getAlarmInfor")
	@ResponseBody
	public DataGridBean<AlarmInfor> getAlarmInfor(AlarmInfor alarmInfor) {
		DataGridBean<AlarmInfor> grid = new DataGridBean<AlarmInfor>();
		if (StringUtils.isNotBlank(alarmInfor.getCurrPage()) && StringUtils.isNotBlank(alarmInfor.getPageSize())) {
			alarmInfor.setStart(
					(Integer.parseInt(alarmInfor.getCurrPage()) - 1) * Integer.parseInt(alarmInfor.getPageSize()));
			alarmInfor.setLimit(Integer.parseInt(alarmInfor.getPageSize()));
		}
		List<AlarmInfor> list = devStatusService.getAlarmInfor(alarmInfor);
		if (list.size() > 0 && list != null) {
			grid.setRows(list);
			grid.setTotal(devStatusService.getAlarmInforListCount(alarmInfor));
		}
		return grid;
	}

	/**
	 * 报警管理>>>报警综合信息 没有分页
	 * 
	 * @return
	 */
	@RequestMapping("/alarm/getAlarmInfor2")
	@ResponseBody
	public MessageBean getAlarmInfor2(String zhanTaiBianHao, String baoJingZhuangTai, String beginDate,
			String endDate) {
		MessageBean mb = new MessageBean();
		Map<String, Object> params = new HashMap<>();
		params.put("zhanTaiBianHao", zhanTaiBianHao);
		params.put("baoJingZhuangTai", baoJingZhuangTai);
		params.put("beginDate", beginDate);
		params.put("endDate", endDate);
		List<AlarmInfor> list = devStatusService.getAlarmInfor2(params);
		if (list != null && list.size() > 0) {
			mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
			mb.setMessage("报警综合信息");
			mb.setData(list);
		} else {
			mb.setCode(StringHelper.MESSAGE_TYPE_NULL);
			mb.setData(null);
		}
		return mb;
	}

	/**
	 * 报警管理>>>报警综合信息 详情列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getAlarmDetail")
	@ResponseBody
	public Map<String, Object> getAlarmDetail(String baoJingBianHao) {
		Map<String, Object> params = new HashMap<>();
		params.put("baoJingBianHao", baoJingBianHao);
		List<AlarmInfor> list = devStatusService.getAlarmDetail(params);
		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}

	/**
	 * 报警管理>>>报警综合信息 详情列表修改保存
	 * 
	 * @return
	 */
	@RequestMapping(value = "/updateAlarmDetail")
	@ResponseBody
	public Map<String, Object> updateAlarmDetail(AlarmInfor alarmInfor, HttpServletRequest request) {
		Map<String, Object> params = new HashMap<>();
		params.put("baoJingBianHao", alarmInfor.getBaoJingBianHao());
		params.put("weiXiuRiQi", alarmInfor.getWeiXiuRiQi());
		params.put("weiXiuReng", alarmInfor.getWeiXiuReng());
		// gongDanZhuangTai 0：未处理 1：处理中 2：搁置 3：已处理
		params.put("gongDanZhuangTai", alarmInfor.getGongDanZhuangTai());
		params.put("weiXiuNeiRong", alarmInfor.getWeiXiuNeiRong());
		params.put("miaoShu", alarmInfor.getMiaoShu());
		params.put("productId", alarmInfor.getProductId());
		if (alarmInfor.getBaoJingZhuangTai().equals("0")) {// BaoJingZhuangTai=0 恢复 BaoJingZhuangTai=1 未处理
			params.put("lifeStatus", "7");// 7：报警恢复
		} else if (alarmInfor.getBaoJingZhuangTai().equals("1")) {
			params.put("lifeStatus", "6");
		}
		params.put("remark", alarmInfor.getMiaoShu());
		String cureateUserId = ParseUtils.getUidFromToken(request, userService);
		params.put("cureateUserId", cureateUserId);
		params.put("taskNo", alarmInfor.getRengWuHao());

		// baoJingZhuangTai 0:已恢复 1：未处理 2：处理中
		String baoJingZhuangTai = "1";
		if (alarmInfor.getGongDanZhuangTai().equals("0")) {// 1：未处理
			baoJingZhuangTai = "1";
		} else if (alarmInfor.getGongDanZhuangTai().equals("1") || alarmInfor.getGongDanZhuangTai().equals("2")) {// 2：处理中
			baoJingZhuangTai = "2";
		} else if (alarmInfor.getGongDanZhuangTai().equals("3")) {// 0:已恢复
			baoJingZhuangTai = "0";
		}
		params.put("baoJingZhuangTai", baoJingZhuangTai);
		// 业务表修改
		int count = devStatusService.updateAlarmDetail(params);
		// 生命周期表(报警表)
		int count2 = devStatusService.updateRtEvent(params);

		Map<String, Object> result = new HashMap<String, Object>();
		if (count >= 0 && count2 > 0) {
			result.put("result", StringHelper.MESSAGE_TYPE_SUCCESS);
		} else {
			result.put("result", StringHelper.MESSAGE_TYPE_ERROR);
		}
		return result;
	}

	/**
	 * 判断用户权限 能够查看哪条线路
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getRoles")
	@ResponseBody
	public Map<String, Object> getRoles(UserRoles userRoles) {
		List<UserRoles> list = devStatusService.getRoles(userRoles);
		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}

	/**
	 * 判断是否发布项目
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getConfigure")
	@ResponseBody
	public Map<String, Object> getConfigure() {
		List<Configure> list = devStatusService.getConfigure();
		JSONArray json = JSONArray.parseArray(JSON.toJSONString(list));
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("result", json);
		return result;
	}

}
